package anonymouschat.client;

import java.io.Serializable;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.SecretKey;

import anonymouschat.security.KeySalt;
import anonymouschat.utils.Print;

/**
 * Identity is an object that is used to store all user information, including
 * public and private keys, user name, and salts.
 * 
 * @author bh349
 * 
 */
public class Identity implements Serializable {

	private static final long serialVersionUID = -2982671647808071976L;

	private String userName;

	private PrivateKey privateRSAKey;

	private PublicKey publicRSAKey;

	private PrivateKey signingKey;

	private PublicKey verKey;

	private SecretKey fileEncKey;

	private SecretKey fileMacKey;

	private KeySalt fileKeySalt;

	/**
	 * 
	 * @param fileEKey
	 *            a key for encrypting files
	 * @param fileMacKey
	 *            a key for MACing files
	 * @param salt
	 *            the salts to use for encryption and MAC
	 * @throws NoSuchAlgorithmException
	 */
	public Identity(SecretKey fileEKey, SecretKey fileMacKey, KeySalt salt)
			throws NoSuchAlgorithmException {
		fileKeySalt = salt;
		fileEncKey = fileEKey;
		this.fileMacKey = fileMacKey;

		KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
		gen.initialize(3072);
		KeyPair keyPair = gen.generateKeyPair();
		publicRSAKey = keyPair.getPublic();
		privateRSAKey = keyPair.getPrivate();

		keyPair = gen.generateKeyPair();
		verKey = keyPair.getPublic();
		signingKey = keyPair.getPrivate();

		String userNameString = PublicIdentity.composeUserName(publicRSAKey,
				verKey);
		Print.debug("New Identity: " + userNameString + " created.");
		userName = userNameString;
	}

	/**
	 * 
	 * @return String - the user name
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @return PrivateKey - the privateRSAKey
	 */
	public PrivateKey getPrivateRSAKey() {
		return privateRSAKey;
	}

	/**
	 * @return PrivateKey - the signingKey
	 */
	public PrivateKey getSigningKey() {
		return signingKey;
	}

	/**
	 * @return PublicKey - the publicRSAKey
	 */
	public PublicKey getPublicRSAKey() {
		return publicRSAKey;
	}

	/**
	 * @return PublicKey - the verKey
	 */
	public PublicKey getVerKey() {
		return verKey;
	}

	/**
	 * @return SecretKey - the fileEncKey
	 */
	public SecretKey getFileEncKey() {
		return fileEncKey;
	}

	/**
	 * @return SecretKey - the Key used to mac local files
	 */
	public SecretKey getFileMacKey() {
		return fileMacKey;
	}

	public KeySalt getFileSalt() {
		return this.fileKeySalt;
	}
}
